<template>
    <div>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="姓名" prop="name">
                <el-input v-model="ruleForm.name"></el-input>
            </el-form-item>
            <el-form-item label="年龄" prop="age">
                <el-input v-model.number="ruleForm.age"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
                <el-button @click="resetForm('ruleForm')">重置</el-button>
                <el-button @click="jump()" >无参数跳转测试</el-button>
                <el-button @click="jump2()" >带参数跳转测试</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>
<script>
    export default {
        data() {
            return {
                ruleForm: {
                    name:'',
                    age:0
                },
                rules: {
                    name: [
                        { required: true, message: '姓名为必填项', trigger: 'blur' },
                        { min: 2, max: 15, message: '长度在 2 到 15 个字符', trigger: 'blur' }
                    ],
                    age: [
                        { required: true, message: '年龄为必填项', trigger: 'change' }
                    ]
                }
            };
        },
        methods: {
            jump(){
                this.$router.push("/pageone")
            },
            jump2(){
                this.$router.push({
                    path:"/pageone",
                    query:{
                        testMessage:"hello"
                    }
                })

            },
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.$message('submit!');
                        const _this=this;
                        console.log(this.ruleForm);
                        axios.post("http://localhost:8011/eurekaclient/student",this.ruleForm).then(function(resp){
                            console.log("提交的结果是："+JSON.stringify(resp));
                            _this.$message("保存成功");
                            _this.$router.push("/pageone")
                        });

                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        }
    }
</script>